package itheima.service.impl;

import itheima.mapper.UserMapper;
import itheima.pojo.User;
import itheima.service.UserService;
import itheima.utils.Md5Util;
import itheima.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User findByUsername(String username) {
        return userMapper.findByUsername(username);
    }

    @Override
    public void register(String username, String password) {

        //密码先加密
        password = Md5Util.getMD5String(password);

        userMapper.register(username,password);
    }

    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }

    @Override
    public void updateAvatar(String avatarUrl) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updateAvatar(avatarUrl,id);
    }

    @Override
    public void updatePwd(String newPwd, String username) {
        newPwd = Md5Util.getMD5String(newPwd);
        userMapper.updatePwd(newPwd,username);
    }
}









































